﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Finance.UI.ViewModels;

namespace Finance.UI
{
    public partial class AccountSettingPage : PhoneApplicationPage
    {
        public AccountSettingPage()
        {
            InitializeComponent();
            Loaded += (o, e) => SetConfigAction();
            Loaded += (o, e) => InilizeDataContext();
        }

        private void SetConfigAction()
        {
            var set = ConfigType.Unknown;
            var value = string.Empty;
            try
            {
                set = (ConfigType)Enum.Parse(typeof(ConfigType), NavigationContext.QueryString["set"], true);
                value = NavigationContext.QueryString["value"];
            }
            catch { }
            if (set == ConfigType.Unknown || string.IsNullOrEmpty(value)) { return; }
            switch (set)
            {
                case ConfigType.PayoutClassification:
                    {
                        App.SettingDataStore.Items.First().PayoutClassificationName = value;
                        break;
                    }
                case ConfigType.IncomeClassification:
                    {
                        App.SettingDataStore.Items.First().IncomeClassificationName = value;
                        break;
                    }
                case ConfigType.Project:
                    {
                        App.SettingDataStore.Items.First().ProjectName = value;
                        break;
                    }
                case ConfigType.Account:
                    {
                        App.SettingDataStore.Items.First().AccountName = value;
                        break;
                    }
                case ConfigType.Trader:
                    {
                        App.SettingDataStore.Items.First().TraderName = value;
                        break;
                    }
                default: break;
            }
        }

        private void InilizeDataContext()
        {
            var dataContext = new SettingPageViewModel(
                new SettingDisplayItemViewModel()
                {
                    Name = "支出类型",
                    CurrentConfigItemName = App.SettingDataStore.Items.First().PayoutClassificationName,
                    //SetConfigPageUri = "/ConfigPages/PayoutClassificationConfig.xaml"
                    SetConfigPageUri = "/SelectorPage.xaml?set=" + ConfigType.PayoutClassification
                    + "&selected=" + App.SettingDataStore.Items.First().PayoutClassificationName,
                },
                new SettingDisplayItemViewModel()
                {
                    Name = "收入类型",
                    CurrentConfigItemName = App.SettingDataStore.Items.First().IncomeClassificationName,
                    //SetConfigPageUri = "/ConfigPages/IncomeClassificationConfig.xaml"
                    SetConfigPageUri = "/SelectorPage.xaml?set=" + ConfigType.IncomeClassification
                    + "&selected=" + App.SettingDataStore.Items.First().IncomeClassificationName,
                },
                new SettingDisplayItemViewModel()
                {
                    Name = "项目类型",
                    CurrentConfigItemName = App.SettingDataStore.Items.First().ProjectName,
                    //SetConfigPageUri = "/ConfigPages/ProjectConfig.xaml"
                    SetConfigPageUri = "/SelectorPage.xaml?set=" + ConfigType.Project
                    + "&selected=" + App.SettingDataStore.Items.First().ProjectName,
                },
                new SettingDisplayItemViewModel()
                {
                    Name = "账户管理",
                    CurrentConfigItemName = App.SettingDataStore.Items.First().AccountName,
                    //SetConfigPageUri = "/ConfigPages/AccountConfig.xaml"
                    SetConfigPageUri = "/SelectorPage.xaml?set=" + ConfigType.Account
                    + "&selected=" + App.SettingDataStore.Items.First().AccountName,
                },
                new SettingDisplayItemViewModel()
                {
                    Name = "商家管理",
                    CurrentConfigItemName = App.SettingDataStore.Items.First().TraderName,
                    //SetConfigPageUri = "/ConfigPages/TraderConfig.xaml"
                    SetConfigPageUri = "/SelectorPage.xaml?set=" + ConfigType.Trader
                    + "&selected=" + App.SettingDataStore.Items.First().TraderName,
                },
                new SettingDisplayItemViewModel()
                {
                    Name = "密码设置",
                    CurrentConfigItemName = App.SettingDataStore.Items.First().SecurityStatus
                    ? "开启"
                    : "关闭",
                    SetConfigPageUri = "/ConfigPages/SecurityConfig.xaml"
                });

            this.DataContext = dataContext;
            App.ReturnLastUri = "/AccountSettingPage.xaml?";
        }

        private void abibSave_Click(object sender, EventArgs e)
        {
            try
            {
                App.SettingDataStore.Commit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Message", MessageBoxButton.OK);
            }
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void abibBack_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}